﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRepository;
using BusinessRule.Organization;
using learun.util;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class CProTZJHRule
    {
        //导入投资计划
        public async static Task<bool> GetImportTZJHList(List<CProTZJH> listjh, List<CProTZJHND> listjz, List<CProTZJHTZF> listtzf)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                for (int i = 0; i < listjh.Count; i++)
                {
                    await irp.Insert(listjh[i]);
                }

                for (int i = 0; i < listjz.Count; i++)
                {
                    await irp.Insert(listjz[i]);
                }

                for (int i = 0; i < listtzf.Count; i++)
                {
                    await irp.Insert(listtzf[i]);
                }

                return true;

            });
        }

        public async static Task<bool> GetSaveFunGL(List<BDicInfoFun> funs, string strope, string uid)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                string strsql = "delete from BDicInfoFun ";//where funid = '" + funs[i].funid.Trim() + "'
                await irp.ExecuteSql(strsql);

                for (int i = 0; i < funs.Count; i++)
                {
                    funs[i].id = CommonHelper.GetUniqueId().ToString();
                    await irp.Insert(funs[i]);
                }
                return true;

            });
        }

        public async static Task<string> GetFunGLListBylx(string lx)
        {
            string strsql = "select * from BDicInfoFun where funid = '" + lx + "'";
            DataTable data = await new RepositoryFactory().BaseRepository().FindTable(strsql);

            string strv = "0";
            for (int i = 0; i < data.Rows.Count; i++)
            {
                strv = data.Rows[i]["nouse"].ToString();
            }
            return strv;
        }

        public async static Task<string> GetFunGLListBylxEunm(string lx)
        {
            string strlx = "";
            if (lx == "投资计划")
            {
                strlx = "investmentplan";
            }
            else if (lx == "概算")
            {
                strlx = "Projectbudget";
            }
            else if (lx == "采购计划")
            {
                strlx = "Purchaserequest";
            }
            else if (lx == "竣工验收计划")
            {
                strlx = "Sitemanagement";
            }
            else if (lx == "月付款计划")
            {
                strlx = "Monthlypaymentplan";
            }
            else if (lx == "合同金额限制")
            {
                strlx = "ContractMoneyLimit";
            }
            else if (lx == "申请金额限制")
            {
                strlx = "PayMoneyLimit";
            }

            string strsql = "select * from BDicInfoFun where funid = '" + strlx + "'";
            DataTable data = await new RepositoryFactory().BaseRepository().FindTable(strsql);

            string strv = "0";
            for (int i = 0; i < data.Rows.Count; i++)
            {
                strv = data.Rows[i]["nouse"].ToString();
            }
            return strv;
        }

        public async static Task<DataTable> GetFunGLList()
        {
            string strsql = "select * from BDicInfoFun ";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        #region 导出
        public static async Task<MemoryStream> GetExportListTS(DataTable exportTable, List<ColumnModel> cols, BaseEditPower.TableName tableName, string Title)
        {
            //List<string> ids = new List<string>();
            //List<ColumnModel> cols = new List<ColumnModel>();

            //取出数据源
            //DataTable exportTable = await ExportList(ids, tableName);


            //设置导出格式
            ExcelConfig excelconfig = new ExcelConfig();
            excelconfig.Title = Title;// "投资计划信息";
            excelconfig.TitleFont = "微软雅黑";
            excelconfig.TitlePoint = 25;
            //excelconfig.HeadPoint = 12;
            excelconfig.FileName = Title + ".xls";
            excelconfig.IsAllSizeColumn = true;
            //每一列的设置,没有设置的列信息，系统将按datatable中的列名导出
            excelconfig.ColumnEntity = new List<ColumnModel>();
            for (int i = 0; i < cols.Count; i++)
            {
                excelconfig.ColumnEntity.Add(new ColumnModel() { Column = cols[i].Column, ExcelColumn = cols[i].ExcelColumn });
            }

            return ExcelHelper.ExportMemoryStream(exportTable, excelconfig);
        }

        /// <summary>
        /// 用户列表（导出Excel）
        /// </summary>
        /// <returns></returns>
        public static async Task<MemoryStream> GetExportList(List<string> ids, List<ColumnModel> cols, BaseEditPower.TableName tableName, string Title)
        {
            //List<string> ids = new List<string>();
            //List<ColumnModel> cols = new List<ColumnModel>();

            //取出数据源
            DataTable exportTable = await ExportList(ids, tableName);


            //设置导出格式
            ExcelConfig excelconfig = new ExcelConfig();
            excelconfig.Title = Title;// "投资计划信息";
            excelconfig.TitleFont = "微软雅黑";
            excelconfig.TitlePoint = 25;
            //excelconfig.HeadPoint = 12;
            excelconfig.FileName = Title + ".xls";
            excelconfig.IsAllSizeColumn = true;
            //每一列的设置,没有设置的列信息，系统将按datatable中的列名导出
            excelconfig.ColumnEntity = new List<ColumnModel>();
            for (int i = 0; i < cols.Count; i++)
            {
                excelconfig.ColumnEntity.Add(new ColumnModel() { Column = cols[i].Column, ExcelColumn = cols[i].ExcelColumn });
            }

            return ExcelHelper.ExportMemoryStream(exportTable, excelconfig);
        }


        public async static Task<DataTable> ExportList(List<string> ids, BaseEditPower.TableName tableName)
        {
            string strsql = "";
            string yids = string.Join("','", ids);
            yids = "'" + yids + "'";
            if (tableName == BaseEditPower.TableName.投资计划)
            {
                strsql = "select * from View_ProTZJHLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.投资计划结转)
            {
                strsql = "select * from View_ProTZJHJZLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.投资计划变更)
            {
                strsql = "select * from View_ProTZJHBGLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.项目)
            {
                strsql = "select * from View_ProLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.采购计划)
            {
                strsql = "select * from View_ProZBCGLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.概算)
            {
                strsql = "select * from View_ProGaiSpcLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.招标)
            {
                strsql = "select * from View_ProZBLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.合同)
            {
                strsql = "select * from View_ProHTLists where dxid in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.合同变更)
            {
                strsql = "select * from View_ProHTBGLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.变更协议)
            {
                strsql = "select * from View_ProHTBGXYLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.付款月计划)
            {
                strsql = "select * from View_ProFKYJHLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.付款申请)
            {
                strsql = "select * from View_ProFKSQLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.日志)
            {
                //View_ProQQXCLists View_ProHTLogLists
                strsql = "select * from View_ProQQXCLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.日志report)
            {
                //View_ProQQXCLists View_ProHTLogLists
                strsql = "select * from View_ProHTLogLists where id in (" + yids + ") and lx = 'report'";
            }
            else if (tableName == BaseEditPower.TableName.日志safe)
            {
                //View_ProQQXCLists View_ProHTLogLists
                strsql = "select * from View_ProHTLogLists where id in (" + yids + ") and lx = 'safe'";
            }
            else if (tableName == BaseEditPower.TableName.结算)
            {
                strsql = "select * from View_ProJSLists where htid in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.竣工)
            {
                strsql = "select * from View_ProJunGongLists where htid in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.用章)
            {
                strsql = "select * from View_ProHTYZLists where id in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.决算)
            {
                strsql = "select * from View_ProJuSLists where xmid in (" + yids + ")";
            }
            else if (tableName == BaseEditPower.TableName.归档)
            {
                strsql = "select * from View_proguidang where xmid in (" + yids + ")";
            }
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        #endregion

        public static string GetXMGLSPZT(string opinion)
        {
            //CHECKED
            //UNCHECK
            //CHECKING
            //RESTART
            //INVALID
            //REJECT

            string ztz = "0";
            if (opinion == "UNCHECK")
            {
                ztz = "0";
            }
            else if (opinion == "DRAFT")//draft
            {
                ztz = "-1";
            }
            else if (opinion == "CHECKING")
            {
                ztz = "1";
            }
            else if (opinion == "INVALID")
            {
                ztz = "4";
            }
            else if (opinion == "CHECKED")
            {
                ztz = "2";
            }
            else if (opinion == "REJECT")
            {
                ztz = "3";
            }
            else if (opinion == "HANDEL")//202109
            {
                ztz = "-1";
            }

            return ztz;
        }

        public static string GetXMGLStatus(string moduleid)
        {
            string strTname = "";
            if (moduleid == "jhnd")
            {
                strTname = BaseEditPower.TableName.投资计划结转.ToString();
            }
            else if (moduleid == "jhbg")
            {
                strTname = BaseEditPower.TableName.投资计划变更.ToString();
            }
            else if (moduleid == "xm")
            {
                strTname = BaseEditPower.TableName.项目.ToString();
            }
            else if (moduleid == "gas")
            {
                strTname = BaseEditPower.TableName.概算.ToString();
            }
            else if (moduleid == "zb")
            {
                strTname = BaseEditPower.TableName.招标.ToString();
            }
            else if (moduleid == "ht")
            {
                strTname = BaseEditPower.TableName.合同.ToString();
            }
            else if (moduleid == "htbg")
            {
                strTname = BaseEditPower.TableName.合同变更.ToString();
            }
            else if (moduleid == "fksq")
            {
                strTname = BaseEditPower.TableName.付款申请.ToString();
            }
            else if (moduleid == "js")
            {
                strTname = BaseEditPower.TableName.结算.ToString();
            }
            else if (moduleid == "jg")
            {
                strTname = BaseEditPower.TableName.竣工.ToString();
            }
            else if (moduleid == "rz")//日志
            {
                strTname = BaseEditPower.TableName.日志.ToString();
            }
            else if (moduleid == "yz")//用章
            {
                strTname = BaseEditPower.TableName.用章.ToString();
            }
            else if (moduleid == "yfkjh")//月付款计划
            {
                strTname = BaseEditPower.TableName.付款月计划.ToString();
            }
            else if (moduleid == "gd")//归档
            {
                strTname = BaseEditPower.TableName.归档.ToString();
            }
            else if (moduleid == "jues")//决算
            {
                strTname = BaseEditPower.TableName.决算.ToString();
            }
            else
            {
                return "";
            }

            return strTname;
        }


        public static async Task<(IEnumerable<dynamic>, int)> GetTZJHList(CProTZJHQuerry querry, string formcode, string strdepid)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.tzxmmc)))
            {
                str = str + " and tzxmmc like '%" + querry.tzxmmc + "%'";
            }

            //if (!(string.IsNullOrEmpty(querry.xmgldw)))
            //{
            //    str = str + " and xmgldwmc like '%" + querry.xmgldw + "%'";
            //}
            //if (!(string.IsNullOrEmpty(querry.xmcqdw)))
            //{
            //    str = str + " and xmcqdw like '%" + querry.xmcqdw + "%'";
            //}
            if (!(string.IsNullOrEmpty(querry.czdw)))
            {
                str = str + " and czdw like '%" + querry.czdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.jsdw)))
            {
                str = str + " and gsid = '" + querry.jsdw + "'";
            }

            if (!(string.IsNullOrEmpty(querry.tzxz)))
            {
                str = str + " and tzxz = '" + querry.tzxz + "'";
            }
            if (!(string.IsNullOrEmpty(querry.tzlx)))
            {
                str = str + " and tzlx = '" + querry.tzlx + "'";
            }
            if (!(string.IsNullOrEmpty(querry.xmxz)))
            {
                str = str + " and xmxz = '" + querry.xmxz + "'";
            }
            if (!(string.IsNullOrEmpty(querry.yearRange)))
            {
                string[] strs = querry.yearRange.Split('_');

                str = str + " and xmkssj >= '" + strs[0] + "'";

                str = str + " and xmjssj <= '" + strs[1] + "'";
            }

            //添加status
            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = '" + querry.status + "'";
            }

            //如果formcode为空，就查询已经审核过的
            if (string.IsNullOrEmpty(formcode))
            {
                str = str + " and spstatus = 'checked'";

                //str = str + " and id not in (select )";
            }
            //可用金额大于0
            if (querry.kyje == "1")
            {
                str = str + " and jhkyye >0";
            }

            if (!string.IsNullOrEmpty(querry.sfbz))
            {
                string strsfbz = querry.sfbz == "新增" ? "0" : "1"; 

                str += " and isnull(sfbz,0) = '"+ strsfbz + "' ";
            }

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProTZJHLists where gsid in " + strdepid + str.Replace("''", "'");

            bool isAsc = false;
            if (querry.sort == "Asc" || querry.sort == "asc")
            {
                isAsc = true;
            }

            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        public async static Task<IEnumerable<dynamic>> GetTZJHList1(List<string> jhids, string strdepid)
        {
            //避免什么都不传
            jhids.Add("autoid10000001");

            string strjhids = "";
            strjhids = string.Join("','", jhids.ToArray());
            //for (int i = 0; i < jhids.Count; i++)
            //{
            //    strjhids
            //}

            string str = "";
            if (!string.IsNullOrEmpty(strjhids))
            {
                strjhids = "'" + strjhids + "'";

                str += " and id in (" + strjhids + ") ";
            }

            string strsql = "select * from View_ProTZJHLists where gsid in " + strdepid + str.Replace("''", "'");

            return await new RepositoryFactory().BaseRepository().FindList(strsql);
        }

        public async static Task<dynamic> GetTZJHList(string jhid)
        {
            string strsql = "select * from View_ProTZJHLists where id = '" + jhid + "'";

            return await new RepositoryFactory().BaseRepository().FindEntity<dynamic>(strsql);
        }

        public async static Task<DataTable> GetTZJHTZFList(string jhid)
        {
            //select* from CProTZJHTZF where jhid = ''
            string strsql = "select * from CProTZJHTZF where jhid = '" + jhid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);


        }



        //根据投资计划id 获取最小年度id
        public async static Task<CProTZJHND> GetTZJHNDidMin(string jhid)
        {
            try
            {
                string strsql = @"select top 1 * from CProTZJHND where jhid = '" + jhid + "' order by jhnd asc";

                CProTZJHND tznd = await new RepositoryFactory().BaseRepository().FindEntity<CProTZJHND>(strsql);
                return tznd;
            }
            catch (Exception)
            {
                throw new Exception("查询异常");
            }
        }

        public async static Task<bool> InsertTZJH(CProTZJH tzjh, CProTZJHND tzjhnd, List<CProTZJHTZF> tzjhtzf, string strope, string Curruid)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                if (strope.ToLower() == "add" || strope == "addtj")
                {
                    //tzjh.createuser = Curruid;

                    //新增进来后台赋值id
                    //tzjh.id = CommonHelper.GetUniqueId().ToString();
                    //tzjh.status = BaseEditPower.GetSPEnumValue("待提交");//这里默认给初始状态



                    await irp.Insert(tzjh);
                    for (int i = 0; i < tzjhtzf.Count; i++)
                    {
                        tzjhtzf[i].id = CommonHelper.GetUniqueId().ToString();
                        tzjhtzf[i].jhid = tzjh.id;
                        await irp.Insert(tzjhtzf[i]);
                    }

                    //tzjhnd.id = CommonHelper.GetUniqueId().ToString();
                    tzjhnd.jhid = tzjh.id;

                    await irp.Insert(tzjhnd);

                }
                else if (strope.ToLower() == "edit" || strope == "edittj")
                {
                    //修改
                    await irp.Update(tzjh);

                    if (null != tzjhnd)//&& !string.IsNullOrEmpty(tzjhnd.id)
                    {
                        string strsql = "delete from CProTZJHND where jhid = '" + tzjh.id + "' and jhnd = '" + tzjhnd.jhnd + "'";
                        await irp.ExecuteSql(strsql);

                        //tzjhnd.id = CommonHelper.GetUniqueId().ToString();
                        tzjhnd.jhid = tzjh.id;
                        await irp.Insert(tzjhnd);
                    }

                    if (null != tzjhtzf && tzjhtzf.Count > 0)
                    {
                        string strsql = "delete from CProTZJHTZF where jhid = '" + tzjh.id + "'";
                        await irp.ExecuteSql(strsql);

                        for (int i = 0; i < tzjhtzf.Count; i++)
                        {
                            tzjhtzf[i].id = CommonHelper.GetUniqueId().ToString();
                            tzjhtzf[i].jhid = tzjh.id;
                            await irp.Insert(tzjhtzf[i]);
                        }
                    }
                }

                return true;

            });
        }

        public async static Task<bool> DeleteTZJH(string jhid, string Curruid)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                string strsql = "delete from CProTZJH where id = '" + jhid + "'";
                await irp.ExecuteSql(strsql);
                strsql = "delete from CProTZJHND where jhid = '" + jhid + "'";
                await irp.ExecuteSql(strsql);
                strsql = "delete from CProTZJHTZF where jhid = '" + jhid + "'";
                await irp.ExecuteSql(strsql);

                return true;

            });
        }

        public async static Task<bool> DeleteTZJH(Root jhids, string Curruid)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                foreach (BaseDeleteMulti idm in jhids.ids)
                {
                    string strsql = "delete from CProTZJH where id = '" + idm.id + "'";
                    await irp.ExecuteSql(strsql);
                    strsql = "delete from CProTZJHND where jhid = '" + idm.id + "'";
                    await irp.ExecuteSql(strsql);
                    strsql = "delete from CProTZJHTZF where jhid = '" + idm.id + "'";
                    await irp.ExecuteSql(strsql);

                }
                return true;

            });
        }

        public static async Task<(IEnumerable<dynamic>, int)> GetTZJHJZList(CProTZJHQuerry querry, string formcode, string strdepid)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.tzxmmc)))
            {
                str = str + " and tzxmmc like '%" + querry.tzxmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.czdw)))
            {
                str = str + " and czdw like '%" + querry.czdw + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.jsdw)))
            {
                str = str + " and gsid = '" + querry.jsdw + "'";
            }

            if (!(string.IsNullOrEmpty(querry.tzxz)))
            {
                str = str + " and tzxz = '" + querry.tzxz + "'";
            }
            if (!(string.IsNullOrEmpty(querry.tzlx)))
            {
                str = str + " and tzlx = '" + querry.tzlx + "'";
            }
            if (!(string.IsNullOrEmpty(querry.xmxz)))
            {
                str = str + " and xmxz = '" + querry.xmxz + "'";
            }

            if (!(string.IsNullOrEmpty(querry.jhnd)))
            {
                str = str + " and jhnd = '" + querry.jhnd + "'";
            }

            //添加status
            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = '" + querry.status + "'";
            }

            if (!(string.IsNullOrEmpty(querry.sfjz)))
            {
                str = str + " and sfjz = '" + querry.sfjz + "'";
            }

            if (!(string.IsNullOrEmpty(querry.yearRange)))
            {
                str = str + " and jhnd = '" + querry.yearRange + "'";
            }

            //if (!(string.IsNullOrEmpty(querry.yearRange)))
            //{
            //    string[] strs = querry.yearRange.Split('_');

            //    str = str + " and jhnd >= '" + strs[0].Split('-')[0] + "'";

            //    str = str + " and jhnd <= '" + strs[1].Split('-')[0] + "'";
            //}

            //如果formcode为空，就查询已经审核过的
            if (string.IsNullOrEmpty(formcode))
            {
                str = str + " and spstatus = 'checked'";
            }

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProTZJHJZLists where gsid in " + strdepid + str.Replace("''", "'");


            bool isAsc = false;
            if (querry.sort == "Asc" || querry.sort == "asc")
            {
                isAsc = true;
            }

            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }



        //获取结转列表
        public async static Task<DataTable> GetTZJHJZList(string jhid)
        {
            string strsql = "select * from View_ProTZJHJZLists where jhid = '" + jhid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }


        public async static Task<dynamic> GetTZJHJZEntity(string jzid)
        {
            string strsql = "select * from View_ProTZJHJZLists where id = '" + jzid + "'";

            return await new RepositoryFactory().BaseRepository().FindEntity(strsql);
        }

        public async static Task<dynamic> GetTZJHJZBefourEntity(string jzid)
        {
            string strsql = @"select * from View_ProTZJHJZLists where jhnd = (
select MAX(jhnd)jhnd from CProTZJHND where jhid = (select jhid from CProTZJHND where id = '" + jzid + @"' )
and jhnd< (select jhnd from CProTZJHND where id = '" + jzid + @"')
) and jhid = (select jhid from CProTZJHND where id = '" + jzid + @"' )";

            return await new RepositoryFactory().BaseRepository().FindEntity(strsql);
        }


        public async static Task<bool> InsertTZJHJZ(CProTZJHND tzjhjz, string uid)
        {
            string strsql = "select * from CProTZJHND where jhid= '" + tzjhjz.jhid + "' and jhnd = '" + tzjhjz.jhnd + "'";
            DataTable dts = await new RepositoryFactory().BaseRepository().FindTable(strsql);
            bool exists = false;
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                exists = true;
            }
            if (!exists)
            {
                await new RepositoryFactory().BaseRepository().Insert(tzjhjz);

                return true;
            }
            else
            {
                throw new Exception("该投资计划" + tzjhjz.jhnd + "年度已经存在");
            }
        }

        public async static Task<bool> UpdateTZJHJZ(CProTZJHND tzjhjz, string uid)
        {
            if (await new RepositoryFactory().BaseRepository().Update(tzjhjz) > 0)
            {
                return true;
            }
            else
            {
                throw new Exception("投资计划结转编号不存在");
            }
        }

        public async static Task<bool> DeleteTZJHJZ(string jzid, string uid)
        {
            string strsql = "delete from CProTZJHND where id = '" + jzid + "'";
            if (await new RepositoryFactory().BaseRepository().ExecuteSql(strsql) > 0)
            {
                return true;
            }
            else
            {
                throw new Exception("投资计划结转编号不存在");
            }
        }


        public static async Task<(IEnumerable<dynamic>, int)> GetTZJHBGIndexList(CProTZJHQuerry querry, string formcode, string strdepid)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.tzxmmc)))
            {
                str = str + " and tzxmmc like '%" + querry.tzxmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.czdw)))
            {
                str = str + " and czdw like '%" + querry.czdw + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.tzxz)))
            {
                str = str + " and tzxz = '" + querry.tzxz + "'";
            }
            if (!(string.IsNullOrEmpty(querry.tzlx)))
            {
                str = str + " and tzlx = '" + querry.tzlx + "'";
            }

            if (!(string.IsNullOrEmpty(querry.jhnd)))
            {
                str = str + " and jhnd = '" + querry.jhnd + "'";
            }

            //添加status
            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = '" + querry.status + "'";
            }

            //如果formcode为空，就查询已经审核过的
            if (string.IsNullOrEmpty(formcode))
            {
                str = str + " and spstatus = 'checked'";
            }

            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProTZJHBGLists where gsid in " + strdepid + str.Replace("''", "'");
            bool isAsc = false;
            if (querry.sort == "Asc" || querry.sort == "asc")
            {
                isAsc = true;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        //获取结转列表
        public async static Task<DataTable> GetTZJHBGList(string jhjzid)
        {
            string strsql = @"";

            strsql = "select * from View_ProTZJHBGLists where jhndid = '" + jhjzid + "'";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        //获取结转列表
        public async static Task<DataTable> GetTZJHBGListByjhid(string jhid)
        {
            string strsql = @"";

            strsql = "select * from View_ProTZJHBGLists where jhndid in (select id from CProTZJHND where jhid = '" + jhid + "' )";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        public async static Task<DataTable> GetTZJHBGListByjzid(string jzid)
        {
            string strsql = @"";

            strsql = "select * from View_ProTZJHBGLists where jhndid = '" + jzid + "' ";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        //获取结转列表
        public async static Task<dynamic> GetTZJHBGByid(string bgid)
        {
            string strsql = @"";

            strsql = "select * from View_ProTZJHBGLists where id = '" + bgid + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(strsql);
        }

        public async static Task<bool> InsertTZJHBG(CProTZJHbgS tzjhbg, string uid)
        {
            string strsql = "select * from CProTZJHND where id= '" + tzjhbg.jhndid + "'";
            DataTable dts = await new RepositoryFactory().BaseRepository().FindTable(strsql);
            bool exists = false;
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                exists = true;
                break;
            }
            if (!exists)
            {
                throw new Exception("该投资计划结转id:" + tzjhbg.jhndid + "不存在经存在");
            }
            else
            {

                //tzjhbg.status = BaseEditPower.GetSPEnumValue("待提交");

                if (await new RepositoryFactory().BaseRepository().Insert(tzjhbg) > 0)
                    return true;
                else
                {
                    throw new Exception("操作失败，影响行数为0 ");
                }
            }
        }

        public async static Task<bool> UpdateTZJHBG(CProTZJHbgS tzjhbg, string uid)
        {
            if (await new RepositoryFactory().BaseRepository().Update(tzjhbg) > 0)
            {
                return true;
            }
            else
            {
                throw new Exception("操作失败，影响行数为0");
            }
        }

        public async static Task<bool> DeleteTZJHBG(string bgid, string uid)
        {
            string strsql = "delete from CProTZJHbgS where id = '" + bgid + "'";
            if (await new RepositoryFactory().BaseRepository().ExecuteSql(strsql) > 0)
            {
                return true;
            }
            else
            {
                throw new Exception("操作失败，影响行数为0");
            }
        }
    }
}
